import baseInfo from '@/config/base.js';
console.log(baseInfo.baseUrl)

let requestTask = null;
let hasLoginModal = false; // false 尚未有打开的登录对话框； true 已有打开的登录对话框
const noTipArr = ['pwd', '请求成功', '验证码', '失效'];
const noLoginArr = ['api/user/Resume/resume_check']

function HTTP(obj, config) {
	let defaultConfig = {
		isRes: true,
		loading: true
	}
	config = { ...defaultConfig,
		...config
	}

	// 如果需要显示loading,mask防止点击穿透
	config.loading && uni.showLoading({
		title: '加载中',
		mask: true
	});

	return new Promise((resolve, reject) => {
		let options = {
			url: "",
			method: "GET",
			timeout: 10000,
			data: {},
			dataType: "json",
			header: {
				"content-type": 'application/x-www-form-urlencoded', //"application/json", // 

				"token": uni.getStorageSync('token') || ""
			},
			success: (res) => {
				uni.hideLoading();
				// 状态码为200
				if (res.statusCode == 200) {
					let data = res.data;

					//返回 { code:10000,msg:"消息",data:[] }
					if (config.isRes) {
						// data.rTask = requestTask
						resolve(data)
					}
					if(res.data.code == 1){
					
					}else{
						
					}
				} else {
					if(res.data.code == 401){
						uni.showModal({
							title: '提示',
							content: '当前未登录，是否去登陆？',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url:'/pages/my/login'
									})
								} else if (res.cancel) {
					
								}
							}
						});
					}else{
						uni.showToast({
							title: "HTTP:状态码异常",
							icon: "none",
						})
						setTimeout(() => {
							uni.hideLoading();
						}, 1300)
						reject("HTTP:状态码异常！");
					}

				}
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: "网络异常，请稍后再试!",
					icon: "none",
				})
				reject("网络异常，请稍后再试!");
			},
			complete: () => {
				requestTask = null;
			}
		}

		options = {
			...options,
			...obj
		};

		const OPENID = uni.getStorageSync("openId");
		if (OPENID) options["header"]["openId"] = OPENID;

		if (options.url && options.method) {
			requestTask = uni.request(options);
		} else {
			uni.showToast({
				title: 'HTTP：缺失参数',
				icon: "none",
				duration: 2000
			})
		}
	})

}

export default {
	GET(url, data = {}, config) {
		url = baseInfo.baseUrl + baseInfo.apiPath +url;
		return HTTP({
			url,
			data,
			method: "GET"
		}, config);
	},
	POST(url, data = {}, config) {
		url = baseInfo.baseUrl + baseInfo.apiPath +url;
		return HTTP({
			url,
			data,
			method: "POST"
		}, config);
	},

	POSTformdata(url, data = {}, config) {
		url = baseInfo.baseUrl + baseInfo.apiPath +url;
		return HTTP({
			url,
			data,
			method: "POST"
		}, config);
	}
}
